﻿using System;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Xml.Linq;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKMenuAssist.DataManagement;
using YJKMenuAssist.Utility;

namespace YJKMenuAssist.MenuManageControls
{
	public partial class RightList : System.Windows.Controls.UserControl, IStyleConnector
	{
		public LeftList LeftList { get; set; }

		public CenterList CenterList { get; set; }

		public CommandTableInfos CommandTableInfos { get; set; }

		public TabItem TabKey { get; internal set; }

		public RightList()
		{
			this.InitializeComponent();
		}

		public void IniTreeData(string strFileName)
		{
			this.m_DicLstCmdInfo.Clear();
			List<string> list = new XmlManager(strFileName).ReadMenuXml();
			XElement rootNode = XElement.Load(strFileName);
			for (int i = 0; i < list.Count; i++)
			{
				this.m_DicLstCmdInfo.Add(list[i], new List<CommandInfos>());
			}
			this.IniTreeView(strFileName);
			foreach (string text in this.m_DicLstCmdInfo.Keys)
			{
				TreeViewItem treeViewItem = this.AddNode(text, false);
				foreach (CommandInfos commandInfos in this.m_DicLstCmdInfo[text])
				{
					this.AddChildNode(commandInfos.CmdName, commandInfos, treeViewItem);
					this.NextIsSplit(commandInfos, treeViewItem, rootNode);
				}
			}
		}

		private void NextIsSplit(CommandInfos commandInfos, TreeViewItem treeViewItem, XElement rootNode)
		{
			foreach (XElement xelement in from target in rootNode.Descendants("PushButton")
			where target.Attribute("Name").Value == commandInfos.CmdName && target.Attribute("ClassName").Value == commandInfos.ClassName && target.Attribute("CommandId").Value == commandInfos.CmdId && target.Parent.Parent.Attribute("Name").Value == "Custom"
			select target)
			{
				XElement xelement2 = (XElement)xelement.NextNode;
				if (xelement2 != null && xelement2.Attribute("Name").Value == "分割线")
				{
					TreeViewItem newItem = this.CreateSplitItem();
					treeViewItem.Items.Add(newItem);
				}
			}
		}

		private TreeViewItem CreateSplitItem()
		{
			TreeViewItem treeViewItem = new TreeViewItem();
            System.Windows.Shapes.Rectangle rectangle = new System.Windows.Shapes.Rectangle();
			rectangle.StrokeDashArray = new DoubleCollection
			{
				1.0,
				1.0
			};
			rectangle.Width = 101;
			rectangle.Stroke = Brushes.Black;
			rectangle.StrokeThickness = 2.0;
			rectangle.Fill = new SolidColorBrush(Color.FromArgb(100, 170, 170, 170));
			rectangle.VerticalAlignment = VerticalAlignment.Center;
			treeViewItem.ContextMenu = (base.FindResource("TreeMenu") as System.Windows.Controls.ContextMenu);
			treeViewItem.Header = rectangle;
			treeViewItem.Tag = "分割线";
			return treeViewItem;
		}

		private void IniTreeView(string strFileName)
		{
			new List<CommandInfos>();
			for (int i = 0; i < this.CommandTableInfos.TableInofList.Count; i++)
			{
				TableInfos tableInfos = this.CommandTableInfos.TableInofList[i];
				string tableName = tableInfos.TableName;
				for (int j = 0; j < tableInfos.CommandInfoList.Count; j++)
				{
					if (!tableInfos.CommandInfoList[j].CmdShow)
					{
						this.IsShow(tableInfos.CommandInfoList[j], strFileName);
					}
					if (tableInfos.CommandInfoList[j].LstCommandInfos.Count > 0)
					{
						foreach (CommandInfos commandInfos in tableInfos.CommandInfoList[j].LstCommandInfos)
						{
							if (!commandInfos.CmdShow)
							{
								this.IsShow(commandInfos, strFileName);
							}
						}
					}
				}
			}
		}

		private void IsShow(CommandInfos commandInfos, string strFileName)
		{
			try
			{
				foreach (XElement xelement in from target in XElement.Load(strFileName).Descendants("PushButton")
				where target.Attribute("Name").Value == commandInfos.CmdName && target.Attribute("ClassName").Value == commandInfos.ClassName && target.Attribute("CommandId").Value == commandInfos.CmdId && target.Parent.Parent.Attribute("Name").Value == "Custom"
				select target)
				{
					this.m_DicLstCmdInfo[xelement.Parent.Attribute("Name").Value].Add(commandInfos);
				}
			}
			catch
			{
			}
		}

		public TreeViewItem AddNode(string sName, bool isCreate)
		{
			TreeViewItem treeViewItem = new TreeViewItem();
			treeViewItem.MouseDoubleClick += this.TreeViewItem_MouseDoubleClick;
			TextBlock textBlock = new TextBlock();
			System.Windows.Controls.TextBox textBox = new System.Windows.Controls.TextBox();
			textBox.Text = sName;
			textBox.Visibility = Visibility.Collapsed;
			textBlock.Text = sName;
			treeViewItem.Header = new Grid
			{
				ContextMenu = (base.FindResource("TreeMenu") as System.Windows.Controls.ContextMenu),
				Children = 
				{
					textBlock,
					textBox
				}
			};
			treeViewItem.Tag = sName;
			this.treeView.Items.Add(treeViewItem);
			if (isCreate)
			{
				treeViewItem.MouseDoubleClick -= this.TreeViewItem_MouseDoubleClick;
				textBlock.Visibility = Visibility.Collapsed;
				textBox.Visibility = Visibility.Visible;
				(this.treeView.Items.GetItemAt(this.treeView.Items.Count - 1) as TreeViewItem).Focus();
				treeViewItem.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
				textBox.LostFocus += this.TextBox_LostFocus;
				this.TabKey.Visibility = Visibility.Hidden;
			}
			return treeViewItem;
		}

		public bool AddChildNode(string sName, CommandInfos commandInfos, TreeViewItem treeViewItem)
		{
			if (treeViewItem == null)
			{
				treeViewItem = (this.treeView.SelectedItem as TreeViewItem);
			}
			TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
			if (treeViewItem != null && treeViewItem2 == null)
			{
				TreeViewItem treeViewItem3 = new TreeViewItem();
				treeViewItem3.Tag = commandInfos;
				TextBlock textBlock = new TextBlock();
				textBlock.Text = sName;
				treeViewItem3.Header = new Grid
				{
					ContextMenu = (base.FindResource("TreeMenu") as System.Windows.Controls.ContextMenu),
					Children = 
					{
						textBlock
					}
				};
				treeViewItem.Items.Add(treeViewItem3);
				return true;
			}
			return false;
		}

		public void AddSplit()
		{
			TreeViewItem treeViewItem = this.treeView.SelectedItem as TreeViewItem;
			TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
			if (treeViewItem != null && treeViewItem2 != null && treeViewItem.Tag != null && treeViewItem.Tag.ToString() != "分割线")
			{
				TreeViewItem treeViewItem3 = treeViewItem2.Items.GetItemAt(treeViewItem2.Items.IndexOf(treeViewItem) + 1) as TreeViewItem;
				if (treeViewItem3.Tag != null && treeViewItem3.Tag.ToString() != "分割线")
				{
					TreeViewItem insertItem = this.CreateSplitItem();
					treeViewItem2.Items.Insert(treeViewItem2.Items.IndexOf(treeViewItem) + 1, insertItem);
				}
			}
		}

		private List<TreeViewItem> DeleteNode()
		{
			List<TreeViewItem> list = new List<TreeViewItem>();
			try
			{
				TreeViewItem treeViewItem = this.treeView.SelectedItem as TreeViewItem;
				TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
				if (treeViewItem2 == null)
				{
					if (treeViewItem.Items.Count == 0 || YJKMessageBox.Show("该目录下存在功能命令，请问是否确定删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.None) == DialogResult.OK)
					{
						foreach (object obj in ((IEnumerable)treeViewItem.Items))
						{
							TreeViewItem item = (TreeViewItem)obj;
							list.Add(item);
						}
						this.treeView.Items.Remove(treeViewItem);
					}
				}
				else
				{
					treeViewItem2.Items.Remove(treeViewItem);
					list.Add(treeViewItem);
				}
			}
			catch
			{
			}
			return list;
		}

		public void DeleteNode(LeftList leftList, CenterList centerList, CommandTableInfos commandTableInfos)
		{
			List<TreeViewItem> list = this.DeleteNode();
			if (list.Count > 0)
			{
				try
				{
					for (int i = 0; i < list.Count; i++)
					{
						(list[i].Tag as CommandInfos).CmdShow = true;
					}
					PropertyNodeItem propertyNodeItem = leftList.tvProperties.SelectedItem as PropertyNodeItem;
					centerList.RefreshContral(propertyNodeItem.DisplayName, commandTableInfos);
				}
				catch
				{
				}
			}
		}

		private void EditNode(System.Windows.Controls.TextBox textBox)
		{
			textBox.Visibility = Visibility.Visible;
			textBox.Focus();
			textBox.SelectAll();
			textBox.LostFocus += this.TextBox_LostFocus;
			this.TabKey.Visibility = Visibility.Hidden;
		}

		private void TreeViewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			try
			{
				TreeViewItem treeViewItem = (TreeViewItem)sender;
				treeViewItem.MouseDoubleClick -= this.TreeViewItem_MouseDoubleClick;
				System.Windows.Controls.TextBox textBox = this.FindVisualChild<System.Windows.Controls.TextBox>(treeViewItem);
				this.EditNode(textBox);
				this.FindVisualChild<TextBlock>(treeViewItem).Visibility = Visibility.Collapsed;
				this.treeView.MouseMove -= this.treeView_MouseMove;
				e.Handled = true;
			}
			catch
			{
			}
		}

		private void TextBox_LostFocus(object sender, RoutedEventArgs e)
		{
			System.Windows.Controls.TextBox textBox = sender as System.Windows.Controls.TextBox;
			TreeViewItem treeViewItem = (TreeViewItem)(textBox.Parent as Grid).Parent;
			if (textBox.Text != string.Empty && this.IsRepetitionName(textBox.Text, treeViewItem))
			{
				TextBlock textBlock = this.FindVisualChild<TextBlock>(treeViewItem);
				textBlock.Visibility = Visibility.Visible;
				textBlock.Text = textBox.Text;
				textBox.Visibility = Visibility.Collapsed;
				treeViewItem.Tag = textBox.Text;
				textBox.LostFocus -= this.TextBox_LostFocus;
				treeViewItem.MouseDoubleClick += this.TreeViewItem_MouseDoubleClick;
				this.treeView.MouseMove += this.treeView_MouseMove;
				this.TabKey.Visibility = Visibility.Visible;
				return;
			}
			if (!this.IsMainWindowClose)
			{
				if (textBox.Text == string.Empty)
				{
					YJKMessageBox.Show("名称不能为空请重新输入！");
				}
				else
				{
					YJKMessageBox.Show("名称已存在请重新输入！");
				}
				base.Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(delegate
				{
					textBox.Focus();
				}));
				return;
			}
		}

		private bool IsRepetitionName(string sName, TreeViewItem treeViewItem)
		{
			for (int i = 0; i < this.treeView.Items.Count - 1; i++)
			{
				if ((this.treeView.Items[i] as TreeViewItem).Tag.ToString() == sName && i != this.treeView.Items.IndexOf(treeViewItem))
				{
					return false;
				}
			}
			return true;
		}

		private void ReName()
		{
			try
			{
				TreeViewItem obj = (TreeViewItem)this.treeView.SelectedItem;
				System.Windows.Controls.TextBox textBox = this.FindVisualChild<System.Windows.Controls.TextBox>(obj);
				textBox.Visibility = Visibility.Visible;
				textBox.Focus();
				textBox.SelectAll();
				this.FindVisualChild<TextBlock>(obj).Visibility = Visibility.Collapsed;
				textBox.LostFocus += this.TextBox_LostFocus;
				this.TabKey.Visibility = Visibility.Hidden;
			}
			catch
			{
			}
		}

		private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
		{
			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
			{
				DependencyObject child = VisualTreeHelper.GetChild(obj, i);
				if (child != null && child is childItem)
				{
					return (childItem)((object)child);
				}
				childItem childItem2 = this.FindVisualChild<childItem>(child);
				if (childItem2 != null)
				{
					return childItem2;
				}
			}
			return default(childItem);
		}

		private void miReName_Click(object sender, RoutedEventArgs e)
		{
			this.ReName();
		}

		private void miDelete_Click(object sender, RoutedEventArgs e)
		{
			this.DeleteNode(this.LeftList, this.CenterList, this.CommandTableInfos);
		}

		private void miDown_Click(object sender, RoutedEventArgs e)
		{
			this.NodeDown(0);
		}

		public void NodeDown(int iNumber)
		{
			TreeViewItem treeViewItem = this.treeView.SelectedItem as TreeViewItem;
			TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
			if (treeViewItem2 == null)
			{
				int num = this.treeView.Items.IndexOf(treeViewItem);
				if (num < this.treeView.Items.Count - 1)
				{
					this.treeView.Items.RemoveAt(num);
					this.treeView.Items.Insert(num + 1, treeViewItem);
					(this.treeView.Items.GetItemAt(num + iNumber) as TreeViewItem).Focus();
					return;
				}
			}
			else
			{
				int num2 = treeViewItem2.Items.IndexOf(treeViewItem);
				if (num2 < treeViewItem2.Items.Count - 1)
				{
					treeViewItem2.Items.RemoveAt(num2);
					treeViewItem2.Items.Insert(num2 + 1, treeViewItem);
					(treeViewItem2.Items.GetItemAt(num2 + iNumber) as TreeViewItem).Focus();
				}
			}
		}

		public void NodeUp(int iNumber)
		{
			TreeViewItem treeViewItem = this.treeView.SelectedItem as TreeViewItem;
			TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
			if (treeViewItem2 == null)
			{
				int num = this.treeView.Items.IndexOf(treeViewItem);
				if (num > 0)
				{
					this.treeView.Items.RemoveAt(num);
					this.treeView.Items.Insert(num - 1, treeViewItem);
					(this.treeView.Items.GetItemAt(num - iNumber) as TreeViewItem).Focus();
					return;
				}
			}
			else
			{
				int num2 = treeViewItem2.Items.IndexOf(treeViewItem);
				if (num2 > 0)
				{
					treeViewItem2.Items.RemoveAt(num2);
					treeViewItem2.Items.Insert(num2 - 1, treeViewItem);
					(treeViewItem2.Items.GetItemAt(num2 - iNumber) as TreeViewItem).Focus();
				}
			}
		}

		private void miUp_Click(object sender, RoutedEventArgs e)
		{
			this.NodeUp(0);
		}

		private void treeView_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
		{
			TreeViewItem treeViewItem = RightList.VisualUpwardSearch<TreeViewItem>(e.OriginalSource as DependencyObject) as TreeViewItem;
			if (treeViewItem != null)
			{
				treeViewItem.Focus();
				e.Handled = true;
			}
		}

		private static DependencyObject VisualUpwardSearch<T>(DependencyObject source)
		{
			while (source != null && source.GetType() != typeof(T))
			{
				source = VisualTreeHelper.GetParent(source);
			}
			return source;
		}

		private void treeView_CheckedDrag(object sender, System.Windows.DragEventArgs e)
		{
			if (e.Source is TreeViewItem)
			{
				if ((e.Source as TreeViewItem).Parent != null)
				{
					e.Effects = System.Windows.DragDropEffects.Move;
				}
				else
				{
					e.Effects = System.Windows.DragDropEffects.None;
				}
			}
			e.Handled = true;
		}

		private void treeView_Drop(object sender, System.Windows.DragEventArgs e)
		{
			try
			{
				TreeViewItem treeViewItem = e.Data.GetData("myFormat") as TreeViewItem;
				TreeViewItem treeViewItem2 = ((Grid)(e.Source as TextBlock).Parent).Parent as TreeViewItem;
				TreeViewItem treeViewItem3 = treeViewItem2.Parent as TreeViewItem;
				if (treeViewItem != null && treeViewItem3 != null)
				{
					this.OtherDropNode(treeViewItem2, treeViewItem);
				}
				else if (treeViewItem2 != null && treeViewItem3 != null)
				{
					this.DropNode(treeViewItem2);
				}
			}
			catch
			{
			}
		}

		private void DropNode(TreeViewItem newTreeViewItem)
		{
			TreeViewItem treeViewItem = this.treeView.SelectedItem as TreeViewItem;
			TreeViewItem treeViewItem2 = treeViewItem.Parent as TreeViewItem;
			TreeViewItem treeViewItem3 = newTreeViewItem.Parent as TreeViewItem;
			int num = treeViewItem3.Items.IndexOf(newTreeViewItem);
			if (treeViewItem2.Tag.ToString() != treeViewItem3.Tag.ToString())
			{
				num++;
			}
			treeViewItem2.Items.Remove(treeViewItem);
			treeViewItem3.Items.Insert(num, treeViewItem);
		}

		private void OtherDropNode(TreeViewItem newTreeViewItem, TreeViewItem tviOther)
		{
			TreeViewItem treeViewItem = tviOther.Parent as TreeViewItem;
			CommandInfos commandInfos = tviOther.Tag as CommandInfos;
			if (commandInfos != null)
			{
				if (treeViewItem == null)
				{
					(tviOther.Parent as System.Windows.Controls.TreeView).Items.Remove(tviOther);
				}
				else
				{
					treeViewItem.Items.Remove(tviOther);
				}
				commandInfos.CmdShow = false;
			}
			TreeViewItem treeViewItem2 = newTreeViewItem.Parent as TreeViewItem;
			int num = treeViewItem2.Items.IndexOf(newTreeViewItem);
			treeViewItem2.Items.Insert(num + 1, tviOther);
		}

		private void treeView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
			{
                System.Windows.Point position = e.GetPosition(this.treeView);
				if ((Math.Abs(position.X - this.m_LastMouseDown.X) > 2.0 || Math.Abs(position.Y - this.m_LastMouseDown.Y) > 2.0) && this.treeView.SelectedItem is TreeViewItem && (this.treeView.SelectedItem as TreeViewItem).Parent is TreeViewItem && e.OriginalSource is TextBlock)
				{
					DragDrop.DoDragDrop(this.treeView.SelectedItem as TreeViewItem, sender, System.Windows.DragDropEffects.Move);
				}
			}
		}

		private void treeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			this.m_LastMouseDown = e.GetPosition(this.treeView);
		}

		[DebuggerNonUserCode]
		[GeneratedCode("PresentationBuildTasks", "4.0.0.0")]
		[EditorBrowsable(EditorBrowsableState.Never)]
		void IStyleConnector.Connect(int connectionId, object target)
		{
			if (connectionId == 6)
			{
				EventSetter eventSetter = new EventSetter();
				eventSetter.Event = UIElement.PreviewMouseRightButtonDownEvent;
				eventSetter.Handler = new MouseButtonEventHandler(this.treeView_PreviewMouseRightButtonUp);
				((Style)target).Setters.Add(eventSetter);
			}
		}

		private Dictionary<string, List<CommandInfos>> m_DicLstCmdInfo = new Dictionary<string, List<CommandInfos>>();

		public bool IsMainWindowClose;

        private System.Windows.Point m_LastMouseDown;

		public bool IsEdit;

		private string m_StrFileName = System.IO.Path.Combine(Product.UserDataLocation, "CustomMenu.xml");
	}
}
